package org.ciallo.simplemusicplayer.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.adapter.PlayStatsAdapter;
import org.ciallo.simplemusicplayer.data.model.Song;
import org.ciallo.simplemusicplayer.viewmodel.PlayStatsViewModel;
import org.ciallo.simplemusicplayer.viewmodel.SharedViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jukomu
 * @Description:
 * @Package: org.ciallo.simplemusicplayer.activity
 * @Project: SimpleMusicPlayer
 * @name: PlayStatsActivity
 * @Date: 2025/6/13-07:53
 * @Filename: PlayStatsActivity
 */
public class PlayStatsActivity extends AppCompatActivity {

    private RecyclerView recyclerViewStats;
    private PlayStatsAdapter adapter;
    private TextView tvEmptyStats;
    private PlayStatsViewModel playStatsViewModel;
    private SharedViewModel sharedViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_stats);

        Toolbar toolbar = findViewById(R.id.toolbar_play_stats);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        playStatsViewModel = new ViewModelProvider(this).get(PlayStatsViewModel.class);
        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);

        recyclerViewStats = findViewById(R.id.recyclerView_play_stats);
        tvEmptyStats = findViewById(R.id.textView_empty_play_stats);

        setupRecyclerView();
        observeViewModel();
    }

    private void setupRecyclerView() {
        adapter = new PlayStatsAdapter((song, position) -> {
            List<Song> songList = adapter.getCurrentList().stream()
                    .map(item -> item.song)
                    .collect(Collectors.toList());
            if (sharedViewModel != null && !songList.isEmpty()) {
                sharedViewModel.playSongAtIndex(songList, position);
            }
        });
        recyclerViewStats.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewStats.setAdapter(adapter);
    }

    private void observeViewModel() {
        playStatsViewModel.getSongStats().observe(this, songStats -> {
            if (songStats != null && !songStats.isEmpty()) {
                adapter.submitList(songStats);
                tvEmptyStats.setVisibility(View.GONE);
                recyclerViewStats.setVisibility(View.VISIBLE);
            } else {
                adapter.submitList(new ArrayList<>());
                tvEmptyStats.setVisibility(View.VISIBLE);
                recyclerViewStats.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_stats_menu, menu); // 需要创建菜单
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            onBackPressed();
            return true;
        } else if (itemId == R.id.action_clear_play_stats) {
            showClearConfirmationDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showClearConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setTitle("清空播放统计")
                .setMessage("确定要清空所有播放次数记录吗？此操作不可撤销。")
                .setPositiveButton(R.string.ok, (dialog, which) -> {
                    playStatsViewModel.clearPlayStats();
                    Toast.makeText(this, "播放统计已清空", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton(R.string.cancel, null)
                .show();
    }
}
